﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using Utilities;

namespace BusinessLogicLayer
{
    public enum LoaiHanhDong
    {
        Them,
        Xoa,
        CapNhat
    }
    public class MuonSachManager
    {
        public ActionResultAsList<ChiTietPhieuMuon> LayCTPMTheoMaPM(int maPM)
        {
            ActionResultAsList<ChiTietPhieuMuon> rs = new ActionResultAsList<ChiTietPhieuMuon>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    rs.ObjectList = db.ChiTietPhieuMuons.Include("Sach").Where(p => p.MaPhieuMuon == maPM).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                rs.AddException(ex);
            }
            return rs;
        }

        public ActionResultAsList<PhieuMuon> LayToanBoPM()
        {
            ActionResultAsList<PhieuMuon> rs = new ActionResultAsList<PhieuMuon>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    rs.ObjectList = db.PhieuMuons.Include("DocGia").Include("ChiTietPhieuMuons").ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                rs.AddException(ex);
            }
            return rs;
        }

        public ActionResult ThemCTPhieuMuon(ref ChiTietPhieuMuon ctpm)
        {
            ActionResult rs = new ActionResult();
            using (var transaction = new TransactionScope())
            {
                using (var db = new ThuVienDbContext())
                {
                    try
                    {
                        ctpm = db.ChiTietPhieuMuons.Add(ctpm);
                        new SachManager().CapNhatTrangThaiSach(ctpm.MaSach, "Đang mượn");
                        db.SaveChanges();
                        transaction.Complete();
                    }
                    catch (Exception ex)
                    {
                        Logger.Write(ex);
                        rs.AddException(ex);
                    }
                }
            }
            return rs;
        }

        public ActionResult ThemPhieuMuon(ref PhieuMuon pm)
        {
            ActionResult rs = new ActionResult();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    pm = db.PhieuMuons.Add(pm);
                    db.SaveChanges();
                    int maPM = pm.MaPhieuMuon;
                    pm = db.PhieuMuons.Include("DocGia").Include("ChiTietPhieuMuons").Single(p1 => p1.MaPhieuMuon == maPM);
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                rs.AddException(ex);
            }
            return rs;
        }

        public ActionResult CapNhatPhieuMuon(ref PhieuMuon pm)
        {
            ActionResult rs = new ActionResult();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    int maPM = pm.MaPhieuMuon;
                    var p = db.PhieuMuons.Single(p1 => p1.MaPhieuMuon == maPM);
                    p.MaDocGia = pm.MaDocGia;
                    db.SaveChanges();
                    pm = db.PhieuMuons.Include("DocGia").Include("ChiTietPhieuMuons").Single(p1 => p1.MaPhieuMuon == maPM);
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                rs.AddException(ex);
            }
            return rs;
        }

        public ActionResult GiaHanCTPhieuMuon(ChiTietPhieuMuon ctpm)
        {
            ActionResult rs = new ActionResult();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var ct = db.ChiTietPhieuMuons.Single(p => p.MaPhieuMuon == ctpm.MaPhieuMuon && p.MaSach == ctpm.MaSach);
                    ct.HanTra = ct.HanTra.AddDays(7);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                rs.AddException(ex);
            }
            return rs;
        }

        public ActionResult TraCTPhieuMuon(ChiTietPhieuMuon ctpm)
        {
            ActionResult rs = new ActionResult();
            using (var transaction = new TransactionScope())
            {
                using (var db = new ThuVienDbContext())
                {
                    try
                    {
                        var ct = db.ChiTietPhieuMuons.Single(p => p.MaPhieuMuon == ctpm.MaPhieuMuon && p.MaSach == ctpm.MaSach);
                        ct.NgayTra = DateTime.Now;
                        new SachManager().CapNhatTrangThaiSach(ctpm.MaSach, "Sẵn sàng");
                        db.SaveChanges();
                        transaction.Complete();
                    }
                    catch (Exception ex)
                    {
                        Logger.Write(ex);
                        rs.AddException(ex);
                    }
                }
            }
            return rs;
        }

        public ValidateResult KTraHanTra(ChiTietPhieuMuon ctpm)
        {
            ValidateResult vrs = new ValidateResult();
            if (ctpm.HanTra.Day == DateTime.Now.Day && ctpm.HanTra.Month == DateTime.Now.Month && ctpm.HanTra.Year == DateTime.Now.Year)
                return vrs;
            if (ctpm.HanTra < DateTime.Now)
                vrs.AddMessage("Quá hạn trả!");
            return vrs;
        }

        public ActionResult XoaCTPhieuMuon(ChiTietPhieuMuon ctpm)
        {
            ActionResult rs = new ActionResult();
            using (var transaction = new TransactionScope())
            {
                using (var db = new ThuVienDbContext())
                {
                    try
                    {
                        ChiTietPhieuMuon t = db.ChiTietPhieuMuons.Single(p => p.MaPhieuMuon == ctpm.MaPhieuMuon && p.MaSach == ctpm.MaSach);
                        db.ChiTietPhieuMuons.Remove(t);
                        new SachManager().CapNhatTrangThaiSach(ctpm.MaSach, "Sẵn sàng");
                        db.SaveChanges();
                        transaction.Complete();
                    }
                    catch (Exception ex)
                    {
                        Logger.Write(ex);
                        rs.AddException(ex);
                    }
                }
            }
            return rs;
        }

        public ActionResult XoaPhieuMuon(PhieuMuon pm)
        {
            ActionResult rs = new ActionResult();
            using (var transaction = new TransactionScope())
            {
                using (var db = new ThuVienDbContext())
                {
                    try
                    {
                        foreach (var item in pm.ChiTietPhieuMuons)
                        {
                            XoaCTPhieuMuon(item);
                        }
                        PhieuMuon t = db.PhieuMuons.Single(p => p.MaPhieuMuon == pm.MaPhieuMuon);
                        db.PhieuMuons.Remove(t);
                        db.SaveChanges();
                        transaction.Complete();
                    }
                    catch (Exception ex)
                    {
                        Logger.Write(ex);
                        rs.AddException(ex);
                    }
                }
            }
            return rs;
        }

        public ActionResultAsList<PhieuMuon> TimPMTheoMaDG(string maDG)
        {
            ActionResultAsList<PhieuMuon> rs = new ActionResultAsList<PhieuMuon>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    rs.ObjectList = db.PhieuMuons.Include("DocGia").Include("ChiTietPhieuMuons").Where(p => p.MaDocGia == maDG).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                rs.AddException(ex);
            }
            return rs;
        }

        public ActionResultAsList<PhieuMuon> TimPMTheoMaPM(int maPM)
        {
            ActionResultAsList<PhieuMuon> rs = new ActionResultAsList<PhieuMuon>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    rs.ObjectList = db.PhieuMuons.Include("DocGia").Include("ChiTietPhieuMuons").Where(p => p.MaPhieuMuon == maPM).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                rs.AddException(ex);
            }
            return rs;
        }

        public ValidateResult KTMuonQuaSL(string maDG)
        {
            ValidateResult vrs = new ValidateResult();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var list = db.ChiTietPhieuMuons.Where(p => p.PhieuMuon.MaDocGia == maDG && p.NgayTra == null);
                    if (list.Count() >= 3)
                        vrs.AddMessage("Độc giả có mã " + maDG + " đã mượn tối đa số lượng!");
                }
            }
            catch (Exception ex)
            {
                vrs.AddMessage(ex.Message);
                Logger.Write(ex);
            }
            return vrs;
        }

        public ActionResultAsList<Sach> LayDSSachDuocMuon(int thang, int nam)
        {
            ActionResultAsList<Sach> rs = new ActionResultAsList<Sach>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    var list = db.ChiTietPhieuMuons.Include("Sach").Include("Sach.NXB1").Include("Sach.TheLoai1").Where(p => p.PhieuMuon.NgayMuon.Month == thang && p.PhieuMuon.NgayMuon.Year == nam);
                    List<ChiTietPhieuMuon> l = list.ToList();
                    List<Sach> ls = new List<Sach>();
                    foreach (var item in l)
                    {
                        ls.Add(item.Sach);
                    }
                    rs.ObjectList = ls;
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                rs.AddException(ex);
            }
            return rs;
        }

        public ActionResultAsList<Sach> LayDSSachDuocMuon()
        {
            ActionResultAsList<Sach> rs = new ActionResultAsList<Sach>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    db.Configuration.ProxyCreationEnabled = false;
                    var list = db.Saches.Include("NXB1").Include("TheLoai1").Where(s => s.ChiTietPhieuMuons.Count > 0);
                    rs.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                rs.AddException(ex);
            }
            return rs;
        }

        public ActionResultAsList<DocGia> LayDSDGMuonSach()
        {
            ActionResultAsList<DocGia> rs = new ActionResultAsList<DocGia>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var list = db.DocGias.Where(d => d.PhieuMuons.Count > 0);
                    rs.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                rs.AddException(ex);
            }
            return rs;
        }
    }
}
